﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor; 
using UnityEditor.SceneManagement;
using System;
using UnityEditor.Experimental.GraphView;
using System.IO;

public class UnityExtentionEditor : EditorWindow
{

    string m_name;
    int m_id;
    string description;
    GameObject prefab;
    
    public UnityExtentionEditor()
    {
        this.titleContent = new GUIContent("Extention Editor");
    }
    [MenuItem("Tools/UnityEditor Extention")]
    static void ShowEditorWindow()
    {
        EditorWindow.GetWindow(typeof(UnityExtentionEditor));

    }

    private void OnGUI()
    {
        GUILayout.BeginVertical();

        GUILayout.Space(10);

        GUI.color = Color.green;
        GUI.skin.label.fontSize = 24;
        GUI.skin.label.alignment = TextAnchor.UpperCenter;
        GUI.skin.label.fontStyle = FontStyle.Bold;       
        GUILayout.Label("UnityEditor Extention");
        GUI.color = Color.white;


        GUILayout.Space(10);
        m_name = EditorGUILayout.TextField("Editor Extention Name", m_name);

        GUILayout.Space(10);
        m_id = EditorGUILayout.IntField("Editor Extention ID", m_id);

        GUILayout.Space(10);
        EditorGUILayout.LabelField("Current Scene :\t"+ EditorSceneManager.GetActiveScene().name);

        GUILayout.Space(10);
        EditorGUILayout.LabelField("Current time :\t" + System.DateTime.Now);

        GUILayout.Space(10);
        prefab = (GameObject)EditorGUILayout.ObjectField("Prefab",prefab ,typeof(GameObject),true);

        GUILayout.Space(10);
        GUILayout.BeginHorizontal();
        GUI.skin.label.fontSize = 12;
        GUI.skin.label.alignment = TextAnchor.MiddleLeft;
        GUI.skin.label.fontStyle = FontStyle.Normal;
        GUILayout.Label("description", GUILayout.MaxWidth(80));
        description = EditorGUILayout.TextArea(description, GUILayout.MaxHeight(50));
        GUILayout.EndHorizontal();

        if (GUILayout.Button("Save Btn with ScreenImage"))
        {
            SaveData();
        }

        GUILayout.EndVertical();
    }

    private void SaveData()
    {
        string path = "Assets/zLog/" + m_name;
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        StreamWriter sw = new StreamWriter(path + "/" + m_id.ToString() + ".txt");
        sw.WriteLine(m_name);
        sw.WriteLine(m_id);
        sw.WriteLine(System.DateTime.Now.ToString());
        sw.WriteLine(EditorSceneManager.GetActiveScene().name);
        sw.WriteLine(description);
        sw.Close();
        ScreenCapture.CaptureScreenshot(path+ "/" + m_id.ToString() + ".png");
        AssetDatabase.Refresh();
    }
}
